package com.nxmu.hire.model.param;

import com.nxmu.hire.model.Research;
import com.nxmu.hire.model.enums.CollegeEnum;
import org.springframework.util.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

/**
 * 学院和教研室信息
 */
public class CollegeParam implements Serializable {

    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public CollegeEnum college;

    public List<Research> researches;

    /**
     * 根据 researchId 获取 Research
     * @param researchId
     * @return
     */
    public Research getResearch(Integer researchId){
        if (StringUtils.isEmpty(researchId) || researches == null || !this.researches.isEmpty()) return null;

        Optional<Research> optionalResearch = researches.stream()
                .filter(research -> Objects.equals(researchId, research.getResearchId()))
                .findFirst();

        return optionalResearch.get();
    }

    public static Research getResearch(Map<CollegeEnum, CollegeParam> map, Integer researchId) {
        if (StringUtils.isEmpty(researchId) || map == null || map.isEmpty()) return null;

        for (CollegeParam collegeParam : map.values()) {
            for (Research research : collegeParam.researches) {
                if (Objects.equals(researchId,research.getResearchId())) return research;
            }
        }

//        AtomicReference<List<Research>> researchList = null;
//        map.forEach((collegeEnum,collegeParam) -> {
//             researchList.set(collegeParam.researches.stream()
//                     .filter(re -> Objects.equals(researchId, re.getResearchId()))
//                     .collect(Collectors.toList()));
//             if (researchList.get() != null && !researchList.get().isEmpty()) return;
//        });

        return null;
    }

}
